package com.rong.DAO;

import com.opensymphony.xwork2.ActionContext;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.rong.ORM.ZynCar;
import com.rong.ORM.ZynCarMaintain;
import com.rong.ORM.ZynUser;
/**
 * A data access object (DAO) providing persistence and search support for
 * ZynCarMaintain entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynCarMaintain
 * @author MyEclipse Persistence Tools
 */

public class ZynCarMaintainDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZynCarMaintainDAO.class);
	// property constants
	public static final String CAR_ID = "carId";
	public static final String TYPE = "type";
	public static final String SUM = "sum";
	public static final String PLACE = "place";
	public static final String PERSON = "person";
	public static final String EXTATTR1 = "extattr1";
	public static final String EXTATTR2 = "extattr2";
	public static final String EXTATTR3 = "extattr3";
	public static final String EXTATTR4 = "extattr4";
	public static final String EXTATTR5 = "extattr5";
	public static final String EXTATTR_DB1 = "extattrDb1";
	public static final String EXTATTR_DB2 = "extattrDb2";
	public static final String EXTATTR_DB3 = "extattrDb3";
	public static final String EXTATTR_DB4 = "extattrDb4";

	public void save(ZynCarMaintain transientInstance) {
		log.debug("saving ZynCarMaintain instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().save(transientInstance);
			 ts.commit();
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynCarMaintain persistentInstance) {
		log.debug("deleting ZynCarMaintain instance");
		try {
			Transaction ts = null;
            ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			 ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynCarMaintain findById(java.lang.Integer id) {
		log.debug("getting ZynCarMaintain instance with id: " + id);
		try {
			ZynCarMaintain instance = (ZynCarMaintain) getSession().get(
					ZynCarMaintain.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynCarMaintain instance) {
		log.debug("finding ZynCarMaintain instance by example");
		try {
			List results = getSession().createCriteria(
					"com.rong.DAO.ZynCarMaintain")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynCarMaintain instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from ZynCarMaintain as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCarId(Object carId) {
		return findByProperty(CAR_ID, carId);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findBySum(Object sum) {
		return findByProperty(SUM, sum);
	}

	public List findByPlace(Object place) {
		return findByProperty(PLACE, place);
	}

	public List findByPerson(Object person) {
		return findByProperty(PERSON, person);
	}

	public List findByExtattr1(Object extattr1) {
		return findByProperty(EXTATTR1, extattr1);
	}

	public List findByExtattr2(Object extattr2) {
		return findByProperty(EXTATTR2, extattr2);
	}

	public List findByExtattr3(Object extattr3) {
		return findByProperty(EXTATTR3, extattr3);
	}

	public List findByExtattr4(Object extattr4) {
		return findByProperty(EXTATTR4, extattr4);
	}

	public List findByExtattr5(Object extattr5) {
		return findByProperty(EXTATTR5, extattr5);
	}

	public List findByExtattrDb1(Object extattrDb1) {
		return findByProperty(EXTATTR_DB1, extattrDb1);
	}

	public List findByExtattrDb2(Object extattrDb2) {
		return findByProperty(EXTATTR_DB2, extattrDb2);
	}

	public List findByExtattrDb3(Object extattrDb3) {
		return findByProperty(EXTATTR_DB3, extattrDb3);
	}

	public List findByExtattrDb4(Object extattrDb4) {
		return findByProperty(EXTATTR_DB4, extattrDb4);
	}

	public List findAll() {
		log.debug("finding all ZynCarMaintain instances");
		try {
			String queryString = "from ZynCarMaintain";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynCarMaintain merge(ZynCarMaintain detachedInstance) {
		log.debug("merging ZynCarMaintain instance");
		try {
			Transaction ts = null;
            ts = getSession().beginTransaction();
			ZynCarMaintain result = (ZynCarMaintain) getSession().merge(detachedInstance);
			 ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynCarMaintain instance) {
		log.debug("attaching dirty ZynCarMaintain instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynCarMaintain instance) {
		log.debug("attaching clean ZynCarMaintain instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 查找并返回符合条件的维修记录
	 */

	public List<ZynCarMaintain> findCarQueryByPage(int page, int rowsPerPage,String carnum,Date startdate) {
		
		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();
				
		Criteria crt = getSession().createCriteria(ZynCarMaintain.class);
		   Criteria crtCar = crt.createCriteria("carinst");		     
		crt.addOrder(org.hibernate.criterion.Order.desc("mdate"));
		
		if(carnum!=null&&!carnum.equalsIgnoreCase("")){			//车牌
			crtCar.add(Restrictions.like("num", carnum,MatchMode.ANYWHERE));
		}
	
		if(startdate!=null){			//日期
			crt.add(Restrictions.ge("mdate", startdate));
		}
			
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有
      	 	
       }else{
    	   crt.add(Restrictions.ge("extattr2",String.valueOf(loginUser.getComId())));
       }
		
		
		crt.setMaxResults(rowsPerPage); // 每页最多显示几条
		crt.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
		ts2.commit();
		List<ZynCarMaintain> list = crt.list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("==========comlist by query======"+list.get(i).getId());
			
		}
        
		

		return list;
	}
	/**
	 * 查找并返回所有公司数
	 */

	public List<ZynCarMaintain> carTatolRows(String carnum,Date startdate) {
		
		getSession().clear();

		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
			
			Criteria crt = getSession().createCriteria(ZynCarMaintain.class);
			   Criteria crtCar = crt.createCriteria("carinst");		     
			crt.addOrder(org.hibernate.criterion.Order.desc("mdate"));
			
			if(carnum!=null&&!carnum.equalsIgnoreCase("")){			//车牌
				crtCar.add(Restrictions.like("num", carnum,MatchMode.ANYWHERE));
			}
		
			if(startdate!=null){			//日期
				crt.add(Restrictions.ge("mdate", startdate));
			}
				
			//--权限判断,分公司只能看本公司单据，本部可以查看全部		
			if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有
	      	 	
	       }else{
	    	   crt.add(Restrictions.ge("extattr2",String.valueOf(loginUser.getComId())));
	       }
			
		List<ZynCarMaintain> list = crt.list();
		return list;
	}
	
}